import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:zootix/apis/zt-api.dart' as Api;
import 'package:zootix/models/zt_coin_goods.model.dart';
import 'package:zootix/util/zt_convert.dart';
import 'package:zootix/util/zt_logger_util.dart';

import '../../util/zt_util.dart';

class ZtGoodsController extends GetxController {
  RxList<ZtCoinGoodsModel> goodsList = <ZtCoinGoodsModel>[].obs;
  @override
  void onReady() {
    ZtLogManger().putLog(
      ZtLogType.globalBehavior,
      ZtLogManger.buildEvent(
          logType: ZtLogType.globalBehavior, page: ZtLogPages.purchase),
    );
    getGoodsList();
    super.onReady();
  }

  Future<void> getGoodsList() async {
    await ZtLoadingUtil.show(status: 'Loading...');
    try {
      goodsList.clear();
      final response = await Api.getGoodsList();
      if (response.data != null) {
        goodsList.addAll(toList(response.data['data'])
            .map((e) => ZtCoinGoodsModel.fromJson(e))
            .toList());
      }
    } catch (e) {
      ZtLoggerUtil().error('Init failed, please try again.${e}');
    } finally {
      await EasyLoading.dismiss();
    }
  }
}
